<?php

class Group_AdminController extends Zend_Controller_Action
{
	public function indexAction()
	{
	}

	public function createAction()
	{
		if (Account_Model_Session::hasValidSession() === FALSE) {
			$fmsg = uniqid();
			$flashMessenger = $this->_helper->FlashMessenger;
			$flashMessenger->setNamespace($fmsg);
			$flashMessenger->addMessage('You need to be logged in to create group');
			$this->_redirect('/login?fmsg=' . $fmsg .'&return='. urlencode($_SERVER['REQUEST_URI']));
		}

		$flashMessages = array();

		$request = $this->getRequest();
		$form    = new Group_Form_Create();

		if ($request->isPost()) {

			if ($form->isValid($request->getPost())) {

				$post = $request->getPost();

				$groupInfo = array();
				$groupInfo['idname'] = $post['idname'];
				$groupInfo['tagline'] = $post['tagline'];
				$groupInfo['about'] = $post['profile_about'];

				$groupReg = new Group_Model_Registration();
				$groupId = $groupReg->register($groupInfo);

				if (intval($groupId) > 0) {

					Euso_Model_Log::push('group', 'registration.succeeded', NULL,
						array('id' => $groupId, 'idname' => $groupInfo['idname']));

					if (empty($groupInfo['tagline']) === FALSE || empty($groupInfo['about']) === FALSE) {
						$profile = new Profile_Model_Data();
						$profile->setOwnerClassIdName('group');
						$profile->setOwnerId($groupId);
						if (empty($groupInfo['tagline']) === FALSE) {
							$profile->setData('tagline', $groupInfo['tagline']);
						}
						if (empty($groupInfo['about']) === FALSE) {
							$profile->setData('about', $groupInfo['about']);
						}
					}


					if (Kernel::getInstance()->isModuleActive('activity')) {
						Activity_Model_Logger::push('user', $creator->getId(), 'created',
							array(
								array('class'=>'group', 'id'=>$groupId, 'role'=>'object')
							)
						);
					}


					//CHECK: show message or redirect to the group's page?

					$group = Group_Model_Entry::getInstanceById($groupId);

					if ($group != NULL) {
						$this->_redirect($group->getUrl());
					} else {
						$this->_redirect('/');
					}

				} else {
					$flashMessenges[] = 'The group name is not available for use';

					Euso_Model_Log::push('group', 'registration.failed', 'Name not available',
						array('idname' => $groupInfo['idname']));
				}
			} else {
				Euso_Model_Log::push('group', 'registration.failed', 'Form has invalid entry(s)',
					array('idname' => $groupInfo['idname']));
			}
		}


		if ($this->_getParam('fmsg')) {
			$flashMessenger = $this->_helper->FlashMessenger;
			$flashMessenger->setNamespace($this->_getParam('fmsg'));
			$this->view->flashMessages = $flashMessenger->getMessages();
		}
		if (is_array($flashMessages)) {
			if (is_array($this->view->flashMessages)) {
				$this->view->flashMessages = array_unique(array_merge($this->view->flashMessages, $flashMessages));
			} else {
				$this->view->flashMessages = $flashMessages;
			}
		}


		$this->view->form = $form;

		Kernel::getInstance()->setHeadTitle('Create a Group');

		$this->view->singleWidgetPageTitle = 'Create a Group';
		$this->view->singleWidgetPage = TRUE;
	}
}